package com.sk.weichat.ui.me.wallet.viewmode;

import android.util.Log;
import com.sk.weichat.AppConstant;
import com.sk.weichat.bean.Code;
import com.sk.weichat.ui.base.CoreManager;
import com.sk.weichat.ui.me.wallet.view.SmsCodeForIdentifyView;
import com.xuan.xuanhttplibrary.okhttp.HttpUtils;
import com.xuan.xuanhttplibrary.okhttp.callback.BaseCallback;
import com.xuan.xuanhttplibrary.okhttp.result.ObjectResult;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import okhttp3.Call;

public class SmsCodeForIdentifyViewMode {

    private SmsCodeForIdentifyView mSmsCodeForIdentifyView;
    private CoreManager coreManager;

    public SmsCodeForIdentifyViewMode(CoreManager coreManager,SmsCodeForIdentifyView mSmsCodeForIdentifyView) {
        this.mSmsCodeForIdentifyView = mSmsCodeForIdentifyView;
        this.coreManager = coreManager;
    }

    /**
    * 发送验证码
    * */
    public void sendCode() {
        mSmsCodeForIdentifyView.showProgress();
        Map<String, String> params = new HashMap<>();
        String language = Locale.getDefault().getLanguage();
        params.put("language", language);
        params.put("areaCode", AppConstant.areaCode);
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        HttpUtils.post().url(coreManager.getConfig().SMS_FOR_RESET_PASSWORD)
                .params(params)
                .build()
                .execute(new BaseCallback<Code>(Code.class) {

                    @Override
                    public void onResponse(ObjectResult<Code> result) {
                        mSmsCodeForIdentifyView.sendCodeResult(result);
                        mSmsCodeForIdentifyView.hideProgress();
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        Log.e("sendCode", "Exception", e);
                        mSmsCodeForIdentifyView.showLoadFailMsg(e.toString());
                        mSmsCodeForIdentifyView.hideProgress();
                    }
                });
    }

    /**
    *验证短信验证码是否正确
    * */
    public void checkSms(String code){
        mSmsCodeForIdentifyView.showProgress();
        Map<String, String> params = new HashMap<>();
        params.put("code", code);
        params.put("access_token", coreManager.getSelfStatus().accessToken);
        HttpUtils.post().url(coreManager.getConfig().SMS_FOR_CHECK_PASSWORD)
                .params(params)
                .build()
                .execute(new BaseCallback<String>(String.class) {
                    @Override
                    public void onResponse(ObjectResult<String> result) {
                        mSmsCodeForIdentifyView.checkSmsResult(result);
                        mSmsCodeForIdentifyView.hideProgress();
                    }

                    @Override
                    public void onError(Call call, Exception e) {
                        Log.e("checkSms", "Exception", e);
                        mSmsCodeForIdentifyView.showLoadFailMsg(e.toString());
                        mSmsCodeForIdentifyView.hideProgress();
                    }
                });
    }
}
